package com.springboot.shanhexin.dao;

import com.springboot.shanhexin.entity.place;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Component(value = "placeDao")
public interface placeDao {

    //(景区内)景点：place

    /**
     * 显示总数量
     * @return
     */
    @Select("select count(*) from place")
    int getCount();

    /**
     * 全部分页显示
     * @return
     */
    @Select("select a.*,b.name as ss_name from place a,scenic_spot b where a.ssid=b.id limit #{pos},#{size}")
    List<place> findByPage(@Param("pos")int pos, @Param("size")int size);

    /**
     * 显示搜索的总数量
     * @return
     */
    @Select("select count(*) from place a,scenic_spot b where a.ssid=b.id" +
            " and ${key} like concat(concat('%',#{name}),'%')")
    int getsearchCount(@Param("key")String key,@Param("name")String name);

    /**
     * 搜索并分页显示
     * @return
     */
    @Select("select a.*,b.name as ss_name from place a,scenic_spot b where a.ssid=b.id" +
            " and ${key} like concat(concat('%',#{name}),'%') limit #{pos},#{size}")
    List<place> findsearchByPage(@Param("key")String key,@Param("name")String name,@Param("pos")int pos, @Param("size")int size);

    /**
     * 新增
     */
    @Insert("insert into place(ssid,name,latitude,longitude,img1,img2,img3,introduce) " +
            "values(#{ssid},#{name},#{latitude},#{longitude},#{img1},#{img2},#{img3},#{introduce})")
    void add(place place);

    /**
     * 删除
     */
    @Delete("delete from place where id=#{id}")
    void delete(@Param("id")int id);

    /**
     * 修改(先找到并显示，再修改)
     */
    @Select("select a.*,b.name as ss_name from place a,scenic_spot b where a.ssid=b.id and a.id=#{id}")
    List<place> getUserone(@Param("id")int id);

    @Update("update place set ssid=#{ssid},name=#{name},latitude=#{latitude},longitude=#{longitude}," +
            "img1=#{img1},img2=#{img2},img3=#{img3},introduce=#{introduce} where id=#{id}")
    void update(place place);

}
